<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试名称" prop="examName">
        <el-input
          v-model="queryParams.examName"
          placeholder="请输入考试名称"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDeleteBatch()"
          :disabled="multiple"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="examCfgList"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="80"/>
      <el-table-column
        label="考试名称"
        align="center"
        prop="examName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考试课程"
        align="center"
        prop="courseName"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="考试班级"
        align="center"
        prop="className"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考试开始时间"
        align="center"
        prop="examBeginTime"
        width="160"
      >
      </el-table-column>
      <el-table-column
        label="考试结束时间"
        align="center"
        prop="examEndTime"
        width="160"
      >
      </el-table-column>
      <el-table-column
        label="考试教室"
        align="center"
        prop="examRoom"
        width="160"
      >
      </el-table-column>
      <el-table-column
        label="监考老师"
        align="center"
        prop="invigilationTeacher"
        width="80"
      >
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope" >
          <span v-if="scope.row.pid">
            <el-button
            size="mini"
            circle
            type="info"
            icon="el-icon-document-copy"
            @click="handleCheckExam(scope.row)"
          /> 
          </span>
          <span v-else-if="!scope.row.pid">
            <el-button
            size="mini"
            circle
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          />
          <el-button
            size="mini"
            circle
            type="info"
            icon="el-icon-connection"
            @click="handleEnterMakeUpExamScores(scope.row)"
          />
          </span>

          <el-button
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          />

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="60%">
          <el-table :data="examStudents" >
            <el-table-column label="序号" align="center" type="index" />
            <el-table-column
              label="学生姓名"
              align="center"
              prop="studentName"
            />
            <el-table-column label="学号" align="center" prop="studentNo" />
            <el-table-column label="学生分数" align="center" prop="score" />
            <el-table-column label="是否通过" align="center" prop="isPass">
              <template slot-scope="scope" >
                <el-radio v-model="scope.row.isPass" label="0">不通过</el-radio>
                <el-radio v-model="scope.row.isPass" label="1">通过</el-radio>
              </template>
            </el-table-column>
          </el-table>
    </el-dialog>
  </div>
</template>
  
  <script>
import { listAllBClass } from "@/api/basedata/bclass";
import { findAllCourse } from "@/api/basedata/bcourse";
import {
  pageList,
  editExam,
  deleteExam,
  deleteBatchExam,
  updateBatchExam,
} from "@/api/exam/examManage";
import {
  getResitExamStudents
} from "@/api/exam/examResultEnter";
import { getClassTeacherList } from "@/api/system/user";

export default {
  name: "examCfg",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      rowList: [],
      // 非单个禁用
      single: true,
      isUpdate: false,
      isAdd: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 数据列表
      examCfgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班主任
      userOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className:undefined,
        examName:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examName: [
          { required: true, message: "考试名称不能为空", trigger: "blur" },
        ],
      },
      courseTypeOptions: [],
      examStudents: [],
      classOptions: [],
    };
  },
  created() {
    this.getList();
    this.getClassTeacherList();
    this.getClassList();
    this.getAllCourse();
  },

  methods: {
    //查询班级
    getClassList() {
      listAllBClass().then((resp) => {
        this.classOptions = resp.data;
      });
    },
    getAllCourse() {
      findAllCourse().then((response) => {
        this.courseTypeOptions = response.data;
      });
    },
    selectCourse(selVal) {
            let { value, label } = selVal;
            this.form.courseId = value;
            this.form.courseName = label;
    },
    selectClass(selVal) {
            let { value, label } = selVal;
            this.form.classId = value;
            this.form.className = label;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then((response) => {
        this.examCfgList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    //查询监考老师
    getClassTeacherList() {
      getClassTeacherList().then((response) => {
        this.userOptions = response.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form.classIds = [];
      this.form = {
        id: undefined,
        examName: undefined,
        curriculumType: undefined,
        classId: null,
        className: null,
        examBeginTime: undefined,
        examEndTime: undefined,
        examRoom: undefined,
        invigilationTeacher: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleCheckExam(row) {
      this.open = true;
      this.title = "查看补考成绩";
      getResitExamStudents(row.id).then((response) => {
        this.examStudents = response.data;
        this.form.examStudents = this.examStudents;
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path:"/academic/examResultEnter", query:{examRecord: '0'}});
      // console.log(this.form);
      
      // this.isAdd = true;
      // this.isUpdate = false;
      // this.reset();
      // console.log(this.form);
      // this.open = true;
      // this.title = "添加考试信息";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.rowList = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path:"/academic/examResultEnter",query:{id:row.id, examRecord: 0}});
      // this.reset();
      // this.isAdd = false;
      // this.isUpdate = true;
      // this.open = true;
      // this.form = JSON.parse(JSON.stringify(row));
      // this.title = "修改考试信息";
    },
    handleUpdateResitExam(row) {
      this.$router.push({path:"/academic/examResultEnter",query:{id:row.id, examRecord: row.examRecord}});
    },
    handleEnterMakeUpExamScores(row) {
      this.$router.push({path:"/academic/examResultEnter",query:{id:row.id, examRecord:Number(row.examRecord) + 1}});
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          editExam(this.form).then((resp) => {
            this.msgSuccess("提交成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return deleteExam(row);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },

    //修改状态
    operationStatus(flag) {
      let str = "启用";
      if (flag == 0) {
        str = "停用";
      }
      for (let i = 0; i < this.rowList.length; i++) {
        this.rowList[i].status = flag;
      }

      updateBatchExam(this.rowList).then((resp) => {
        this.msgSuccess(str + "成功");
        this.getList();
      });
    },
    //批量删除
    handleDeleteBatch() {
      this.$confirm("是否确认删除选择的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteBatchExam(this.rowList).then((resp) => {
            this.getList();
            this.msgSuccess("删除成功");
          });
        })
        .catch(function () {});
    },
  },
};
</script>
  